package com.zxl.always.on.display.ui.widget.clock;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.zxl.always.on.display.R;
import com.zxl.always.on.display.constant.LockerEnv;
import com.zxl.always.on.display.model.ClockStyle;
import com.zxl.always.on.display.model.bean.ClockInfo;
import com.zxl.always.on.display.tools.FileUtil;
import com.zxl.always.on.display.tools.LogUtils;

import java.io.File;

/**
 * Created by Administrator on 2018/3/2.
 */

public class ImageClockView extends BaseClockView {

    private ImageView mImageView;

    public ImageClockView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mImageView = findViewById(R.id.setting_theme_clock_image);
        refreshStyle();
    }

    public ImageView getImageView() {
        return mImageView;
    }

    @Override
    public void refreshStyle() {
        if (null == mImageView) {
            LogUtils.d("mImageView == null");
            return;
        }
        try {
            mImageView.setImageBitmap(BitmapFactory.decodeFile(LockerEnv.Path.IMAGE_SHOW));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public Bitmap getSettingPreview(int itemSize) {
        try {

            if (FileUtil.isFileExist(LockerEnv.Path.IMAGE_SHOW)) {
                mImageView.setImageBitmap(BitmapFactory.decodeFile(LockerEnv.Path.IMAGE_SHOW));
            } else {
                mImageView.setImageResource(R.drawable.setas_thumb);
            }
        } catch (Throwable e) {
            e.printStackTrace();
            mImageView.setImageResource(R.drawable.setas_thumb);
        }
        return super.getSettingPreview(itemSize);
    }

    public static void savePath() {
        if (FileUtil.isFileExist(LockerEnv.Path.IMAGE_TMP)) {
            new File(LockerEnv.Path.IMAGE_TMP).renameTo(new File(LockerEnv.Path.IMAGE_SHOW));
        }
        new ClockInfo(ClockStyle.CLOCK_IMAGE).saveData();
    }

}
